package com.txf.coomon.net.net_okhttp.api;

import android.net.Uri;

import com.txf.net_okhttp3library.HttpQueue;
import com.txf.net_okhttp3library.HttpRequest;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.Request;

/**
 * @author txf
 * @create 2018/12/12 0012
 * @app请求任务 聚合类
 */

public class ApiRequestTask {
    public static String rootUri = "http://shopapi.stage.kankanai.com.cn/v1";

    /**
     * 添加通用请求头
     */
    public static Request addCoomonHeader(Request request) {
        Headers headers = new Headers
                .Builder()
                .add("Authorization", "Bearer " + "eyJhbGciOiJIUzUxMiIsImlhdCI6MTU0ODkyMzQ5MiwiZXhwIjozMDk3ODU0MTg0LjY4NDg5MTd9.eyJpYXQiOjE1NDg5MjM0OTIuNjg0ODkxNywidWlkIjoiNWM0NDVmNzUwMWNhNDMwYmVlYjQ0OWYzIiwidXNlcm5hbWUiOiJ0b21hdG8iLCJ0eXBlIjowLCJyIjpmYWxzZX0.U4xpdQ1fXwmDLSgiXl_GxGmdPaHZid6iynMoTY6XNlsuznSYzftFTdtnUITaQKDBvA7dXp3IGTXDGLgFnYQSKA")
                .build();
        return request.newBuilder().headers(headers).build();
    }


    /**
     * 消息列表
     */
    public static void requestMsgs(int tag, HashMap<String, Object> maps, Callback l) {
        HttpQueue.newHttpQueue().cancelRequest(tag);
        String url = rootUri + "/msgs";
//            3.7.1 消息列表
//            • 请求地址
//            GET /msgs
//            • 请求参数
//            参数名称 类型 是否必须 默认值 描述
//            page int N 1 ⻚码
//            offset int N 10 每⻚数量
//            mtype int N 空 消息类型，0:vip到店，1:⿊名单到店, 2: ⼊侵记录
//            date string N 空 抓拍⽇期，针对抓拍时间进⾏

        Uri.Builder builder = mergingParameters(url, maps);

        Request request = HttpRequest.createRequest(builder.build().toString(), HttpRequest.GET);
        HttpQueue.newHttpQueue().addRequest(tag, addCoomonHeader(request), l);
    }

    private static Uri.Builder mergingParameters(String url, HashMap<String, Object> maps) {
        Uri.Builder builder = Uri.parse(url).buildUpon();
        for (Map.Entry<String, Object> entry : maps.entrySet()) {
            builder.appendQueryParameter(entry.getKey(), String.valueOf(entry.getValue()));
        }
        return builder;
    }

}
